using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine;
using UnityEngine.Networking;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void intCallback(int value);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void strCallback(string value);

public class JsCallAsync : MonoBehaviour
{
    #region 异步js

    [DllImport("__Internal")]
    public static extern void AddNumbers(int x, int y,
        [MarshalAs(UnmanagedType.FunctionPtr)] intCallback result);


    [DllImport("__Internal")]
    public static extern void UnityAsync(int x, int y,
        [MarshalAs(UnmanagedType.FunctionPtr)] intCallback result);

    //
    [DllImport("__Internal")]
    public static extern void UnityAsyncStr(string key, string content,
        [MarshalAs(UnmanagedType.FunctionPtr)] strCallback result);


    [DllImport("__Internal")]
    public static extern void UnityAsyncStr2(string key, string content,
        [MarshalAs(UnmanagedType.FunctionPtr)] strCallback result);


    [DllImport("__Internal")]
    public static extern string TestJsStr();
    
    [DllImport("__Internal")]
    public static extern string TestJsStr2(string key);

    #endregion

    [MonoPInvokeCallback(typeof(intCallback))]
    public static void ShowResult(int value)
    {
        Debug.Log("1 +++async js back result:" + value);
    }

    [MonoPInvokeCallback(typeof(intCallback))]
    public static void ShowResult2(int value)
    {
        Debug.Log("2 +++async js back result:" + value);
    }

    [MonoPInvokeCallback(typeof(strCallback))]
    public static void ResultStr(string value)
    {
        Debug.Log($"0 +++async js back result===>key:{0},v:{value}");
    }

    [MonoPInvokeCallback(typeof(strCallback))]
    public static void ResultStr3(string value)
    {
        Debug.Log($"3 +++async js back result===>key:{0},v:{value}");
    }

    private int x;
    private int y;
    private string key = "hello";
    private string content = "test";

    private void OnGUI()
    {
        // if (GUILayout.Button("JsCall.UnityAsync"))
        // {
        //     AddNumbers(123, 789, ShowResult);
        // }
        GUILayout.Label($"x:{x},y:{y}");
        x = (int)GUILayout.HorizontalSlider(x, -2000, 2000, GUILayout.Width(400));
        y = (int)GUILayout.HorizontalSlider(y, -2000, 2000, GUILayout.Width(400));
        if (GUILayout.Button("JsCall.UnityAsync"))
        {
            UnityAsync(x, y, ShowResult);
        }

        if (GUILayout.Button("JsCall.UnityAsync2"))
        {
            UnityAsync(x, y, ShowResult2);
        }

        if (GUILayout.Button("JsCall.UnityAsync3"))
        {
            UnityAsync(x, y, JsCallAsyncBack.ShowResult);
        }

        GUILayout.Label($"key:{key},content:{content}");
        key = GUILayout.TextArea(key, GUILayout.Width(200));
        content = GUILayout.TextArea(content, GUILayout.Width(400));
        if (GUILayout.Button("JsCall.UnityAsync4 string"))
        {
            UnityAsyncStr(x.ToString(), y.ToString(), ResultStr);
        }

        if (GUILayout.Button("JsCall.UnityAsync5 string"))
        {
            UnityAsyncStr2(x.ToString(), y.ToString(), ResultStr3);
        }

        if (GUILayout.Button("JsCall.UnityAsync6 string"))
        {
            var str = TestJsStr();
            Debug.Log(str);
        }
    }
}